package com.example.jsonarry.nanningtravel;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.MotionEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.example.jsonarry.nanningtravel.adapter.MyStaggedRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity
{
    @Bind(R.id.toolbar) Toolbar toolbar;
//    @Bind(R.id.fab) FloatingActionButton fab;
    @Bind(R.id.recyclerView) RecyclerView recyclerview;
    private Snackbar snackbar;
    private List<String> list;
    //	private MyRecyclerAdapter adapter;
    private MyStaggedRecyclerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        setSupportActionBar(toolbar);
        /*fab.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                snackbar = Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", new View.OnClickListener()
                        {
                            @Override
                            public void onClick(View v)
                            {
                                showToast("snackBar", Toast.LENGTH_LONG);
                            }
                        });
                snackbar.show();
            }
        });*/

        initData();
        //设置线性布局管理器--垂直、水平
//		recyclerview.setLayoutManager(new LinearLayoutManager(this));
//		recyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
//		recyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, true));
//		recyclerview.setLayoutManager(new GridLayoutManager(this, 3));
        recyclerview.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        adapter = new MyStaggedRecyclerAdapter(this, list);
        recyclerview.setAdapter(adapter);

        recyclerview.setItemAnimator(new DefaultItemAnimator());

        //设置条目点击事件
        adapter.setOnItemClickListener(new MyStaggedRecyclerAdapter.OnItemClickListener() {

            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(getApplicationContext(), "条目"+position+"被点击", Toast.LENGTH_SHORT).show();
            }
        });
        adapter.setOnItemClickLongListener(new MyStaggedRecyclerAdapter.OnItemLongClickListener() {

            @Override
            public void onItemLongClick(View view, int position) {
                Toast.makeText(getApplicationContext(), "条目"+position+"被长按~~", Toast.LENGTH_SHORT).show();
                adapter.deleteData(position);
            }
        });
    }
    private void initData()
    {
        list = new ArrayList<>();
        for (int i = 0; i < 50; i++)
        {
            list.add(i+"crystal");
        }
    }
    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        if (snackbar!=null)
        {
            snackbar.dismiss();
        }
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings)
        {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
